package cn.st4rlight.util.page;

import java.util.List;

import com.google.common.collect.Lists;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author st4rlight <st4rlight@163.com>
 * Created on 2024-05-05
 */
@Data
@ApiModel(description = "分页结果")
public class PageResult<T> {

    @ApiModelProperty(value = "当前页码")
    private Integer pageNo;

    @ApiModelProperty(value = "每页条数")
    private Integer pageSize;

    @ApiModelProperty(value = "总记录数")
    private Integer totalCount;

    @ApiModelProperty(value = "分页数据")
    private List<T> data = Lists.newArrayList();


    public static <T> PageResult<T> buildFrom(PageWrapper<T> pageWrapper) {
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setPageNo(pageWrapper.getPageNo());
        pageResult.setPageSize(pageWrapper.getPageSize());
        pageResult.setTotalCount(pageWrapper.getTotalCount());
        pageResult.setData(pageWrapper.getDataList());
        return pageResult;
    }

    public int countTotalPage() {
        return pageSize > 0 ? (totalCount / pageSize) + (totalCount % pageSize == 0 ? 0 : 1) : 0;
    }
}
